import { defineStore } from 'pinia'
import store from 'store2'
/* 
    展示数据列表的情况。
*/
const useWebsStore = defineStore('websStore', {
    state: () => {
        return {
            websites: [],
        }
    },
    actions: {
        addWebs(item) {
            // 去重。
            const index = this.websites.findIndex(it => it.url === item.url)
            if (index === -1) {
                this.websites.unshift(item)
                store('websites', this.websites)
            } else {
                myApi.alertMsg('该网站已添加过！！！')
            }
        },
        init() {
            // 初始化获取数据
            this.websites = store.get('websites')
        },
        delWebs(url) {
            this.websites = this.websites.filter(it => it.url !== url)
            store('websites', this.websites)
        }
    },
    getters: {
        find() { // 根据keywords 过滤出数据内容
            return (keywords) => {
                if (keywords === '') return this.websites
                const result = this.websites.filter((item) => {
                    let partten = new RegExp(keywords, 'i')
                    return partten.test(item.title)
                })
                return result
            }
        }
    }
})

export default useWebsStore;
